<?php
/**
 * Created by PhpStorm.
 * User: 吴登科
 * Date: 2018/11/09
 * Time: 14:42
 * 我的
 */

namespace Home\Controller;

use Think\Controller;
use think\Session;

class CarController extends Controller
{
    public $customer_id = '';
    public $store_id = 0;

    /*
     * 初始化
     */
    public function _initialize()
    {
        if (session('?customer_id') && session('?STORE_ID')) {
            $this->customer_id = session('customer_id');//用户id
            $this->store_id = session('STORE_ID');//店铺id
        }
        $this->customer_id = "20181102115031877722";
        $this->store_id = '11';
    }

    /**
     * 建议反馈页面
     */
    public function index(){
        $this->display();
    }

    /**
     * 添加菜品到购物车操作
     */
    public function addCart()
    {
        if($this->customer_id && $this->store_id){

            $dishes_id = I('post.dishes_id');
            $data['customer_id'] = $this->customer_id;
            $data['store_id'] = $this->store_id;
            $data['good_id'] = $dishes_id;
            $data['time'] = time();
            $data['category_name'] = I('post.category_name');
            $data['category_id'] = I('post.category_id');
            $data['good_name'] = I('post.good_name');
            $data['good_price'] = I('post.good_price');
            $data['good_pic'] = I('post.good_pic');
            $data['count'] = I('post.count');
            $data['practice'] = I('post.practice');//菜品的属性
            $data['all_price'] = I('post.all_price');

            $result = M('car')->add($data);

            if ($result) {
                //加入购物车成功
                $this->ajaxreturn(array('state' => 1, 'data' => $data,'msg'=>'加入购物车成功！'), 'json');
            } else {
                //加入购物车失败
                $this->ajaxreturn(array('state' => -1,'msg'=>'加入购物车失败！'), 'json');
            }

        }else{
            $this->error("服务器繁忙请稍后再试！", U('Home/Index/index'));
        }
    }

    /**
     *获取购物车详情
     */
    public function carList()
    {
        if($this->customer_id && $this->store_id){
            $result = M('car')->where("customer_id='$this->customer_id'")->select();

            if($result){
                $this->ajaxreturn(array('state' => 1,'data' => $result,'msg'=>'获取购物车数据成功！'), 'json');
            }else{
                $this->ajaxreturn(array('state' => 2,'data' => [],'msg'=>'购物车为空！'), 'json');
            }

        }else{
            $this->error("服务器繁忙请稍后再试！", U('Home/Index/index'));
        }
    }

    /**
     * 删除购物车
     */
     public function delCar(){

         if($this->customer_id && $this->store_id){
             $car_id = I('post.car_id');
             $result = M('car')->where("id='$car_id'")->delete();
             if ($result) {
                 //删除成功
                 $this->ajaxreturn(array('state' => 1,'msg'=>'删除购物车成功！'), 'json');
             } else {
                 //删除失败
                 $this->ajaxreturn(array('state' => -1,'msg'=>'删除购物车失败！'), 'json');
             }
         }else{
             $this->error("服务器繁忙请稍后再试！", U('Home/Index/index'));
         }
     }
}